package com.lznb.cloud.service.impl;

import com.lznb.cloud.controller.JsonResult;
import com.lznb.cloud.entity.NewsHotspots;
import com.lznb.cloud.repository.NewsHotspotsRepository;
import com.lznb.cloud.service.NewsHotspotsService;
import com.lznb.cloud.utils.SQLUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NewsHotspotsServiceImpl implements NewsHotspotsService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private NewsHotspotsRepository newsHotspotsRepository;

    @Override
    public NewsHotspots getNewsHotspots(String hotspotsId) {
        return newsHotspotsRepository.findOne(hotspotsId);
    }

    @Override
    public JsonResult save(NewsHotspots newsHotspots) {
        JsonResult jsonResult=new JsonResult();
        jsonResult.run(jsonResult,newsHotspotsRepository.save(newsHotspots));
        return jsonResult;
    }

    @Override
    public JsonResult update(NewsHotspots newsHotspots) {
        JsonResult jsonResult=new JsonResult();
        jsonResult.run(jsonResult, newsHotspotsRepository.saveAndFlush(newsHotspots));
        return jsonResult;
    }

    @Override
    public JsonResult delete(String hotspotsId) {
        JsonResult jsonResult=new JsonResult();
        newsHotspotsRepository.delete(hotspotsId);
        return jsonResult;
    }

    @Override
    public Map findByPage(Integer pageIndex, Integer size,NewsHotspots newsHotspots) {
        Sort sort = new Sort(Sort.Direction.DESC, "hotspotsId");
        Pageable pageable = new PageRequest(pageIndex, size, sort);
        Specification<NewsHotspots> specification = this.handleSpeci(newsHotspots);
        Page<NewsHotspots> list = newsHotspotsRepository.findAll(specification,pageable);
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.getTotalElements());
        map.put("data",list.getContent());
        return map;
    }


    /***
    * 考查询条件处理<br>
    * @param newsHotspots {@link NewsHotspots} 查询条件实体
    * @return {@link Specification}
    */
    public Specification<NewsHotspots> handleSpeci(NewsHotspots newsHotspots) {
        Specification<NewsHotspots> specification = new Specification<NewsHotspots>() {
            @Override
            public Predicate toPredicate(Root<NewsHotspots> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = SQLUtils.CriteriaSQL(root,newsHotspots,criteriaBuilder);
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return specification;
    }
}
